﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Model;
using System.Collections.ObjectModel;
using System.Threading;

namespace University.Workshop.ServerApp
{
    /// <summary>
    /// Interaction logic for Server.xaml
    /// </summary>
    public partial class Server : Window
    {
        PokerServer server;
        Collection<LogUser> users = new Collection<LogUser>();
        Collection<string> names = new Collection<string>();
        bool status;
        string lastMessage;
        Thread statusThread;
        int Key;
        public void Status()
        {
            while (true)
            {
                if (lastMessage != GlobalString.Instance.Message)
                {
                    StatusLabel.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(delegate() { StatusLabel.Content = GlobalString.Instance.Message; }));
                    lastMessage = GlobalString.Instance.Message;
                }
            }
        }
        
        public Server()
        {
            InitializeComponent();
            server = new PokerServer(textBox1);
            Stop.IsEnabled = false;
            status = true;
       
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            Start.IsEnabled = false;
            Stop.IsEnabled = true;
            button1.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            button4.IsEnabled = true;
            
            GlobalString.Instance.Message = " ";
            lastMessage = GlobalString.Instance.Message;
            Thread ThreadServer;
            ThreadServer = new Thread(server.InitializeServer);
            ThreadServer.Start(3000);
            statusThread = new Thread(Status);
            statusThread.Start();
        
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            server.DisconnectServer();

            server.ShutdownServer();
            statusThread.IsBackground = true;
            GlobalString.Instance.Message = "Served stopped";
            this.Close();
        }
        private void Verify(string keyWord, string key, string name, int level)
        {
            if (status)
            {
                if (keyWord == "Logged")
                {
                    if (key == "on")
                    {
                        names.Add(name);
                        LogUser log = new LogUser(name, level, DateTime.Now);
                        users.Add(log);
                    }
                    else if (key == "out")
                    {
                        int index = names.IndexOf(name);
                        names.Remove(name);
                        users.Remove(users[index]);
                    }
                }
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            server.DisconnectServer();
            server.ShutdownServer();
            statusThread.IsBackground = true;
            GlobalString.Instance.Message = "Served stopped";
         
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            string[] test = DateTime.Now.ToString().Split(' ');
            Key = test.Length;
            if (textBox1.Text != " ")
            {
                Verify(textBox1.Text.Split(' ')[Key + 2], textBox1.Text.Split(' ')[Key + 3], textBox1.Text.Split(' ')[Key], 0);
                listBox1.Items.Add(textBox1.Text);
            }
        
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ActiveUsers active = new ActiveUsers(users, textBox1, names);
            status = false;
            active.ShowDialog();
            status = true;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Banned banned = new Banned();

            banned.ShowDialog();
        
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Unban unban = new Unban();
            unban.ShowDialog();
       
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
